﻿using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    public class PastePartOperation : OperationBase
    {
        private DesignView _designView;
        private List<Tuple<Part, Part, IPartContainer, string>> _pastePartList;
        public PastePartOperation(DesignView designView, Part copiedPart, IPartContainer partContainer)
        {
            _pastePartList = new List<Tuple<Part, Part, IPartContainer, string>>();
            _designView = designView;

            CreatePastePartList(copiedPart, partContainer);
        }

        private void CreatePastePartList(Part copiedPart, IPartContainer partContainer)
        {
            Type type = copiedPart.GetType();
            Part pastePart = (Coolzon.UI.Part)type.Assembly.CreateInstance(copiedPart.GetType().FullName);
            pastePart.Name = _designView.GetPartId();
            pastePart.XFullName = pastePart.Name;
            pastePart.XCaption = pastePart.Name;

            Tuple<Part, Part, IPartContainer, string> tmpPart = new Tuple<Part, Part, IPartContainer, string>(copiedPart, pastePart, partContainer, copiedPart.XGetMetadata().Type);
            _pastePartList.Add(tmpPart);

            if (copiedPart is IPartContainer)
            {
                List<Part> partList = (copiedPart as IPartContainer).GetPartList();
                if (partList != null && partList.Count > 0)
                {
                    foreach (var child in partList)
                    {
                        CreatePastePartList(child, pastePart as IPartContainer);
                    }
                }
            }
        }

        protected override void OnDo()
        {
            if (_pastePartList != null && _pastePartList.Count > 0)
            {
                foreach (var item in _pastePartList)
                {
                    _designView.API_Paste(item.Item1, item.Item2, item.Item3, item.Item4);
                    Type type = item.Item1.GetType();
                    foreach (var property in type.GetProperties())
                    {
                        try
                        {
                            CPPEI.Coolzon.UI.XPropertyAttribute[] attrs = (CPPEI.Coolzon.UI.XPropertyAttribute[])property.GetCustomAttributes(typeof(CPPEI.Coolzon.UI.XPropertyAttribute), false);
                            if (attrs != null && attrs.Length > 0)
                            {
                                if (property.CanWrite && property.CanRead)
                                {
                                    //不设置Left,Top,Width,Height
                                    if (string.Equals(attrs[0].Name, "Left", StringComparison.CurrentCultureIgnoreCase) || string.Equals(attrs[0].Name, "Top", StringComparison.CurrentCultureIgnoreCase) || string.Equals(attrs[0].Name, "Width", StringComparison.CurrentCultureIgnoreCase) || string.Equals(attrs[0].Name, "Height", StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        continue;
                                    }
                                    object objValue = property.GetValue(item.Item1);
                                    if (objValue != null)
                                    {
                                        property.SetValue(item.Item2, objValue);
                                        Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(item.Item2.Metadata, attrs[0].Name, property.PropertyType, objValue);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Common.Logger.ErrorToTag(Library.LogTAG, "API_Copy属性赋值出错,PropertyName:{0},ex:{1}", property.Name, ex);
                        }
                    }
                }
            }
        }

        protected override void OnUndo()
        {
            if (_pastePartList != null && _pastePartList.Count > 0)
            {
                for (int index = _pastePartList.Count - 1; index >= 0; index--)
                {
                    _designView.API_Remove(_pastePartList[index].Item2);
                }
            }
        }
    }
}
